package com.hotel.room.controller;

import cn.hutool.core.convert.NumberWithFormat;
import com.hotel.common.config.SystemConfig;
import com.hotel.common.param.RenewRoomAdd;
import com.hotel.common.param.RoomOrderAdd;
import com.hotel.common.param.ShowMyRoom;
import com.hotel.common.util.JwtUtil;
import com.hotel.common.vo.R;
import com.hotel.room.service.HotelRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 酒店客房信息表 前端控制器
 * </p>
 *
 * @author trc
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/hotel/room")
public class HotelRoomController {

    @Autowired
    private HotelRoomService hotelRoomService;

    @RequestMapping("/show")
    public R showRoomList(@RequestParam("listType") String listType) {
        return hotelRoomService.showRoomList(listType);
    }

    @RequestMapping("/showUserRoom")
    public R showMyRoomList(HttpServletRequest request, @RequestBody ShowMyRoom showMyRoom) {

        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token,"id")).intValue();
        return hotelRoomService.showMyRoomList(id, showMyRoom);

    }

    @RequestMapping("/rate")
    public R showRoomRate() {
        return hotelRoomService.showRoomRate();
    }

    @RequestMapping("/pay")
    public R payRoom(HttpServletRequest request, @RequestBody RoomOrderAdd roomOrderAdd) {

        if (roomOrderAdd == null) {
            return R.fail("数据出错");
        }

        // 根据token获取登陆用户id
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token,"id")).intValue();
        roomOrderAdd.setUserId(id);

        // 根据payType不同,选择不同的支付方式
        switch (roomOrderAdd.getPayType()) {
            case 1:
                return hotelRoomService.payRoomByWallet(roomOrderAdd);
            case 2:
                return hotelRoomService.payRoomByAliPay(roomOrderAdd);
        }
        return R.fail("预约失败");
    }

    @RequestMapping("/renew")
    public R renewRoom(HttpServletRequest request, @RequestBody RenewRoomAdd renewRoomAdd) {

        if (renewRoomAdd == null) {
            return R.fail("数据出错");
        }

        // 根据token获取登陆用户id
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token,"id")).intValue();
        renewRoomAdd.setUserId(id);

        // 根据payType不同,选择不同的支付方式
        switch (renewRoomAdd.getPayType()) {
            case 1:
                return hotelRoomService.renewRoomByWallet(renewRoomAdd);
            case 2:
                return hotelRoomService.renewRoomByAliPay(renewRoomAdd);
        }

        return R.fail("续费失败");
    }

}

